// packagesA/pages/astrategy/index.js
import Toast from '@vant/weapp/toast/toast';
const myRequest = require('../../../utils/request'); // 导入整个模块

const app = getApp()
import {
  createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
  store
} from '../../../store/strategy/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo:{},
    scrollTop:0,
    strategy: [],
    fieldshow:false,
    field:'',
    isShow:false, //emoji盒子，
    isShow: false,//控制emoji表情是否显示
    isLoad: true,//解决初试加载时emoji动画执行一次
    content: "",//评论框的内容
    isLoading: true,//是否显示加载数据提示
    disabled: true,
    cfBg: false,
    emojiChar: "☺-😋-😌-😍-😏-😜-😝-😞-😔-😪-😭-😁-😂-😃-😅-😆-👿-😒-😓-😔-😏-😖-😘-😚-😒-😡-😢-😣-😤-😢-😨-😳-😵-😷-😸-😻-😼-😽-😾-😿-🙊-🙋-🙏-✈-🚇-🚃-🚌-🍄-🍅-🍆-🍇-🍈-🍉-🍑-🍒-🍓-🐔-🐶-🐷-👦-👧-👱-👩-👰-👨-👲-👳-💃-💄-💅-💆-💇-🌹-💑-💓-💘-🚲",
    //0x1f---
    emoji: [
      "01", "02", "03", "04", "05", "06", "07", "08", "09","10", 
      "11", "12", "13", "14", "15", "16", "17", "18", "19","20", 
      "21", "22", "23", "24", "25", "26", "27", "28", "29","30", 
      "31", "32", "33", "34", "35", "36", "37", "38", "39","40", 
      "41", "42", "43", "44", "45", "46", "47", "48", "49","50", 
      "51", "52", "53", "54", "55",
    ],
    focus: false,
    KeyboardHeight:0
  },
  // 滚动事件监听
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop,
    })
  },
    //点击表情显示隐藏表情盒子
    emojiShowHide: function () {
      this.setData({
        isShow: !this.data.isShow,
        isLoad: false,
        cfBg: !this.data.false
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

     // 页面初始化 options为页面跳转所带来的参数
     var em = {}, that = this, emChar = that.data.emojiChar.split("-");
     var emojis = []
     that.data.emoji.forEach(function (v, i) {
       em = {
         char: emChar[i],
         emoji: v
       };
       emojis.push(em)
     });
        that.setData({
         emojis: emojis
       })
    //在onload中创建
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: ['strategy'],
      actions: ['updatestrategy']
    })

   

    this.setData({
      strategy: store.strategy,
      bg: store.strategy.img,
      userinfo:JSON.parse(wx.getStorageSync('userinfo'))
    })
    
  },
  
  updatelike(e) {
    let username = JSON.parse(wx.getStorageSync('userinfo')).username
    let that = this
    let lovedata = this.data.strategy.like
    let collectdata = this.data.strategy.collect
    if (e.currentTarget.dataset.love) {
      //如果是取消点赞，则将其在数组中去除
      if (this.data.strategy.islove) {
        lovedata.forEach((element, index) => {
          if (username == element.username) {
            lovedata.splice(index, 1);
            return;
          }
        });
      } else {
        lovedata.push({
          id: this.data.strategy.id,
          username: username,
          value:5
        })
      }
      myRequest.post('/strategy/getlike', {
        id: this.data.strategy.id,
        data: JSON.stringify(lovedata)
      }).then(res=>{
        that.setData({
          strategy: {
            ...that.data.strategy,
            islove: !that.data.strategy.islove
          }
        })
        if (that.data.strategy.islove) Toast('感谢你的喜欢！');
        else Toast('取消喜欢！');
      })
    

    } else if (e.currentTarget.dataset.collect) {
         //如果是取消收藏，则将其在数组中去除
         if (this.data.strategy.iscollect) {
          collectdata.forEach((element, index) => {
            if (username == element.username) {
              collectdata.splice(index, 1);
              return;
            }
          });
        } else {
          collectdata.push({
            id: this.data.strategy.id,
            username: username
          })
        }
        myRequest.post('/strategy/getcollect', {
          id: this.data.strategy.id,
          data: JSON.stringify(collectdata)
        }).then(res=>{
          that.setData({
            strategy: {
              ...that.data.strategy,
              iscollect: !that.data.strategy.iscollect
            }
          })
          if (that.data.strategy.iscollect) Toast('收藏成功！');
          else Toast('取消收藏！');
        })
      
     

    } else {
      console.log("comment");
    }
    console.log(e);
  },

  // 打开留言弹窗
  gosay(){
    this.setData({ fieldshow: true});
    setTimeout(()=>{
      this.setData({ focus: true });
    },100)
  },


  // // 留言弹窗
  onClose() {
    this.setData({ fieldshow: false });
  },

  bindKeyInput: function (e) {
    this.setData({
      field: e.detail.value
    })
  },

  //表情选择
  emojiChoose: function (e) {
    //当前输入内容和表情合并
    this.setData({
      field: this.data.field + e.currentTarget.dataset.emoji
    })
  },

  //发送留言
  sendfield(e){
    console.log(e.detail.value)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log(111);
    store.updatestrategy({})
    console.log(store.strategy);
  },    

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})